﻿using DevExpress.ClipboardSource.SpreadsheetML;
using DevExpress.DataAccess.Native.Web;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraLayout.Customization;
using SX_MesProject.Dtos.YSDtos;
using SX_MesProject.YS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.TextFormatting;
using static SX_MesProject.HttpClientHelper;
using static SX_MesProject.ProcessManage.ProcedureBindAdd;
using static SX_MesProject.YS.ProductionPlan;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace SX_MesProject.ProductionPlanManagement
{
    public partial class PlanMenuAdd : DevExpress.XtraEditors.XtraForm
    {
        string columnName1 = GlobalData.ColumnName;
        string rowName = GlobalData.RowName;
        private object rowData;
        public delegate void LoadDataPlanAdd();
        public event LoadDataPlanAdd OneLoadDataPlanAdd;
        public PlanMenuAdd()
        {

            InitializeComponent();
            comboBoxEdit3.Properties.Items.Add("生产");
            comboBoxEdit3.Properties.Items.Add("维护");
        }

        private void comboBoxEdit3_Click(object sender, EventArgs e)
        {

        }

        private void checkButton1_CheckedChanged(object sender, EventArgs e)
        {

        }
        public void SetRowData(object data, dynamic columnNames)
        {
            this.rowData = data;
            // 假设 rowData 是一个 DataRowView 类型的数据
            if (this.rowData == null)
            {
                MessageBox.Show("rowData 是 null");
            }
            else
            {
                comboBoxEdit2.Text = rowName;
                textEdit1.Text = columnName1.Substring(0, 10);
                comboBoxEdit1.Text = columnName1.Substring(columnName1.Length - 2);
                comboBoxEdit3.Text = "维护";
            }
        }
        //添加按钮
        private async void checkButton3_CheckedChanged(object sender, EventArgs e)
        {
            var result = await HttpClientHelper.RequestUrl<string, ApiResult<List<ViewOrder>>>("https://localhost:44556/", "GetProductionAddList?EquipmentName=" + rowName + "", MethodType.GET);
            this.gridControl1.DataSource = result.Data;

            // 创建 RepositoryItemRadioGroup 控件
            RepositoryItemRadioGroup radioGroup = new RepositoryItemRadioGroup();
            radioGroup.Items.AddRange(new RadioGroupItem[] {
    new RadioGroupItem(0, "")
});

            // 创建 GridColumn
            GridColumn radioColumn = new GridColumn
            {
                FieldName = "IsSelected",
                Caption = "",
                Visible = true,
                ColumnEdit = radioGroup  // 设置为 RepositoryItemRadioGroup 编辑器
            };

            // 在 Designer 代码中，将 radioColumn 插入到最前面
            this.gridView1.Columns.Insert(0, radioColumn);

            // 可选：设置列宽
            radioColumn.Width = 100;
            
        }

        private void checkButton1_CheckedChanged_1(object sender, EventArgs e)
        {
            // 获取选中的行索引
            int[] selectedRows = gridView1.GetSelectedRows();

            if (selectedRows.Length > 0)
            {
                // 获取选中的第一行数据
                var selectedData = (ViewOrder)gridView1.GetRow(selectedRows[0]);

                DataSourceAdd add = new DataSourceAdd();
                add.name = rowName;
                
                add.date= columnName1.Substring(0, 10);
                add.shit= columnName1.Substring(columnName1.Length - 2);
                add.num = selectedData.plannedQuantity.ToString();
                add.value = rowName + ",计划数量" + selectedData.plannedQuantity.ToString();

                AddData(add);


            }
            else
            {
                MessageBox.Show("Please select a row first!");
            }
        }
        public async void AddData(DataSourceAdd addd)
        {
         var aa=   await HttpClientHelper.RequestUrl<DataSourceAdd, ApiResult<int>>("https://localhost:44556/", "FindOrderDetailAdd", MethodType.POST, addd);
            if (aa.Code == 200)
            {

                MessageBox.Show("计划添加成功");
                this.DialogResult = DialogResult.OK;
                this.Close();  // 关闭子页面

                

            }
            else
            {
                MessageBox.Show("计划添加失败");
            }
        }
        


    }
}